<?php
namespace app\common\service;

use app\common\util\Util;
use think\Model;

class FriendService extends BaseService
{


    /**
     * 微信端: 获取好友列表
     * @param $sid
     * @param $uid
     * @param $p
     * @param $pageSize
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getFriendListByUid($sid, $uid, $p, $pageSize){
        //获取好友表中的uid
        $friends = db('friend')->field('friend_studentsid')->where(['sid' => $sid, 'my_uid' => $uid, 'status' => 1])->select();
        $friends_students_ids = []; //保存好友的学员ID
        if($friends){
            foreach($friends as $k => $v){
                $friends_students_ids[$k] = $v['friend_studentsid'];
            }
        }

        //获取自身班级ID
        $class_service = new ClassesService();
        $classes = $class_service->getOwnClassListByUid($sid, $uid);
        if($classes){
            $class_ids = [];
            foreach($classes as $key => $val){
                $class_ids[$key] = $val['classid'];
            }
            //获取不重复的学员
            $class_students = db('class_students')
                                ->distinct(true)
                                ->field('studentsid,uid')
                                ->where(['sid' => $sid, 'classid' => ['in', $class_ids], 'uid' => ['<>', $uid]])
                                ->select();
            if($class_students){
                foreach($class_students as $kl => $vl){
                    $class_students_ids[$kl] = $vl['studentsid'];
                }
                //合并数组，获取所有的学员ID
                $friends_students_ids = array_merge($friends_students_ids, $class_students_ids);
            }
        }

        $where = [
            's.id' => ['in', $friends_students_ids],
            's.sid' => $sid,
            's.is_counselor' => 0,
            's.status' => 1
        ];
        $field = 's.id as studentsid,s.uid,s.name,s.dynamic_update_time,u.user_nickname,u.user_headimgurl,
        sd.id as dynamic_id,sd.description as dynamic_description,sd.type as dynamic_type';
        $order = 's.dynamic_update_time DESC,s.uid DESC';
        $list = db('students s')
                ->field($field)
                ->join('user u', 'u.user_id=s.uid', 'LEFT')
                ->join('students_dynamic sd', 's.dynamic_id = sd.id', 'LEFT')
                ->where($where)
                ->order($order)
                ->limit(($p-1) * $pageSize, $pageSize)
                ->select();
        if($list){
            foreach($list as $ky => $vy){
                $list[$ky]['dynamic_imgs'] = ''; //保存动态图片

                if($vy['dynamic_id']){
                    if($vy['dynamic_update_time'] > 0){
                        $list[$ky]['dynamic_update_time'] = Util::userDynamicTimeToStr($vy['dynamic_update_time']);
                    }

                    //动态为上传图片
                    if($vy['dynamic_type'] == 3){
                        //获取个人主页上传的照片,最多三张
                        $user_imgs = db('user_students_img')
                                    ->field('img_url')
//                                    ->where(['sid' => $sid, 'uid' => $vy['uid'], 'addtime' => ['>=', $vy['dynamic_update_time']] ,'status' => 1])
                                    ->where(['sid' => $sid, 'uid' => $vy['uid'], 'status' => 1])
                                    ->order('addtime DESC')
                                    ->limit(3)
                                    ->select();
                        if($user_imgs){
                            foreach($user_imgs as $kv => $vv){
                                $imgs[$kv] = $vv['img_url'];
                            }

                            $list[$ky]['dynamic_imgs'] = $imgs;
                        }
                    }
                }
            }
        }

        return $list;
    }
}







